package com.wuxiaoge.schedule;

import jakarta.annotation.Resource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class JobScheduler {

    @Autowired
    private JobLauncher jobLauncher;

    @Resource
    @Qualifier("firstJob")
    private Job firstJob;

    // 每 5 分钟执行一次
    @Scheduled(fixedRate = 30000) // 300000 毫秒 = 5 分钟
    public void runJob() {
        try {
            JobParameters jobParameters = new JobParametersBuilder()
                    .addLong("run.id", System.currentTimeMillis())
                    .toJobParameters();
            jobLauncher.run(firstJob, jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
